<?php

class Smsgrid_Grid {
	protected $examp; //query number
	protected $page; // get the requested page
	protected $limit; // get how many rows we want to have into the grid
	protected $sidx; // get index row - i.e. user click to sort
	protected $sord; // get the direction
	protected $searchOn;
	
	/**
	 * @return the $examp
	 */
	public function getExamp() {
		return $this->examp;
	}
	
	/**
	 * @return the $page
	 */
	public function getPage() {
		return $this->page;
	}
	
	/**
	 * @return the $limit
	 */
	public function getLimit() {
		return $this->limit;
	}
	
	/**
	 * @return the $sidx
	 */
	public function getSidx() {
		return $this->sidx;
	}
	
	/**
	 * @return the $sord
	 */
	public function getSord() {
		return $this->sord;
	}
	
	/**
	 * @return the $searchOn
	 */
	public function getSearchOn() {
		return $this->searchOn;
	}
	
	/**
	 * @param field_type $examp
	 */
	public function setExamp($examp) {
		$this->examp = $examp;
	}
	
	/**
	 * @param field_type $page
	 */
	public function setPage($page) {
		$this->page = $page;
	}
	
	/**
	 * @param field_type $limit
	 */
	public function setLimit($limit) {
		$this->limit = $limit;
	}
	
	/**
	 * @param field_type $sidx
	 */
	public function setSidx($sidx) {
		$this->sidx = $sidx;
	}
	
	/**
	 * @param field_type $sord
	 */
	public function setSord($sord) {
		$this->sord = $sord;
	}
	
	/**
	 * @param field_type $searchOn
	 */
	public function setSearchOn($searchOn) {
		$this->searchOn = $searchOn;
	}
	
	function __construct() {
		$examp = $_REQUEST ["q"]; //query number
		$page = $_REQUEST ['page']; // get the requested page
		$limit = $_REQUEST ['rows']; // get how many rows we want to have into the grid
		$sidx = $_REQUEST ['sidx']; // get index row - i.e. user click to sort
		$sord = $_REQUEST ['sord']; // get the direction
		$searchOn = $this->Strip ( $_REQUEST ['_search'] );
		$this->setSearchOn ( $searchOn );
		$this->setExamp ( $examp );
		$this->setLimit ( $limit );
		$this->setPage ( $page );
		$this->setSidx ( $sidx );
	}
	
	public function Strip($value) {
		if (get_magic_quotes_gpc () != 0) {
			if (is_array ( $value ))
				if ($this->array_is_associative ( $value )) {
					foreach ( $value as $k => $v )
						$tmp_val [$k] = stripslashes ( $v );
					$value = $tmp_val;
				} else
					for($j = 0; $j < sizeof ( $value ); $j ++)
						$value [$j] = stripslashes ( $value [$j] );
			else
				$value = stripslashes ( $value );
		}
		return $value;
	}
	
	function array_is_associative($array) {
		if (is_array ( $array ) && ! empty ( $array )) {
			for($iterator = count ( $array ) - 1; $iterator; $iterator --) {
				if (! array_key_exists ( $iterator, $array )) {
					return true;
				}
			}
			return ! array_key_exists ( 0, $array );
		}
		return false;
	}
	
	public function getWhere() {
		$wh='';
		$searchOn = $this->getSearchOn();
		if ($searchOn == 'true') {
			$sarr = $this->Strip ( $_REQUEST );
			foreach ( $sarr as $k => $v ) {
				switch ($k) {
					case 'id' :
					case 'invdate' :
					case 'name' :
					case 'note' :
						$wh .= " AND " . $k . " LIKE '" . $v . "%'";
						break;
					case 'amount' :
					case 'tax' :
					case 'total' :
						$wh .= " AND " . $k . " = " . $v;
						break;
				}
			}
		}
	}
	
	public function pages($result)
	{
		$page = $this->getPage();
		$count = $result->count ();
		$limit = $this->getLimit();
		if ($count > 0) {
			$total_pages = ceil ( $count / $limit );
		} else {
			$total_pages = 0;
		}
		if ($page > $total_pages)
			$page = $total_pages;
		$start = $limit*$page - $limit; // do not put $limit*($page - 1)
        if ($start<0) $start = 0;
        
        return array($start,$limit,$page,$count);
	}

}

?>